package com.cskaoyan.service.impl;

import com.cskaoyan.bean.*;

import com.cskaoyan.bean.admin.vo.ExpressConfigDataVO;
import com.cskaoyan.bean.wx.vo.*;
import com.cskaoyan.bean.admin.vo.OrderDetailVO;
import com.cskaoyan.bean.admin.vo.OrderReqVO;
import com.cskaoyan.converter.String2DateConverter;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.service.SystemService;
import com.cskaoyan.utils.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author Jcy
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    String2DateConverter string2DateConverter;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    CartMapper cartMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;
    @Autowired
    SystemService systemService;

    /**
     * 订单管理初始化
     *
     * @param orderReq
     * @return
     */
    @Override
    public BasePageData list(OrderReqVO orderReq) {
        MarketPageUtil.startPage(orderReq);
        OrderExample example = new OrderExample();
        example.setOrderByClause(orderReq.getSort() + " " + orderReq.getOrder());
        OrderExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        //根据userId查询
        Integer userId = orderReq.getUserId();
        if (!CommonUtil.isNull(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        //根据订单编号查询
        String orderSn = orderReq.getOrderSn();
        if (!CommonUtil.isNull(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }
        //根据开始和结束日期查询
        String start = orderReq.getStart();
        String end = orderReq.getEnd();
        if (!CommonUtil.isNull(start) && !CommonUtil.isNull(end)) {
            Date startDate = string2DateConverter.convert(start);
            Date endDate = string2DateConverter.convert(end);
            criteria.andAddTimeBetween(startDate, endDate);
        }
        //根据订单状态查询
        Short[] orderStatusArray = orderReq.getOrderStatusArray();
        if (!CommonUtil.isNull(orderStatusArray)) {
            criteria.andOrderStatusIn(Arrays.asList(orderStatusArray));
        }
        List<Order> orders = orderMapper.selectByExample(example);
        return BasePageData.ok(orders);
    }

    /**
     * 订单详情
     *
     * @param id
     * @return
     */
    @Override
    public OrderDetailVO detail(Integer id) {
        //查询订单信息
        Order order = orderMapper.selectByPrimaryKey(id);
        //查询订单的商品信息
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria orderGoodsExampleCriteria = orderGoodsExample.createCriteria();
        orderGoodsExampleCriteria.andOrderIdEqualTo(id);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        //查询订单的用户信息
        User user = userMapper.selectByPrimaryKey(order.getUserId());
        return new OrderDetailVO(orderGoods, user, order);
    }

    /**
     * 删除
     *
     * @param orderId
     * @return
     */
    @Override
    public int delete(Integer orderId) {
        //将订单的deleted修改为true
        Order order = new Order();
        order.setId(orderId);
        order.setEndTime(new Date());
        order.setDeleted(true);
        int affectRows = orderMapper.updateByPrimaryKeySelective(order);
        if (affectRows == 0) {
            return Constant.FAIL;
        }
        //将订单下的商品deleted也修改为true
        OrderGoods orderGoods = new OrderGoods();
        orderGoods.setUpdateTime(new Date());
        orderGoods.setDeleted(true);
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria orderGoodsExampleCriteria = orderGoodsExample.createCriteria();
        orderGoodsExampleCriteria.andOrderIdEqualTo(orderId);
        int affectRowsSon = orderGoodsMapper.updateByExampleSelective(orderGoods, orderGoodsExample);
        if (affectRowsSon == 0) {
            return Constant.FAIL;
        }
        return Constant.SUCCESS;
    }

    /**
     * 退款
     *
     * @param orderId
     * @param refundMoney
     */
    @Override
    public void refund(Integer orderId, BigDecimal refundMoney) {
        Order order = new Order();
        order.setId(orderId);
        //暂时修改这个字段,前台还要再修改
        order.setUpdateTime(new Date());
        order.setRefundAmount(refundMoney);
        order.setRefundTime(new Date());
        order.setOrderStatus(Constant.ORDER_REFUNDED);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    /**
     * 发货
     *
     * @param orderId
     * @param shipChannel
     * @param shipSn
     */
    @Override
    public void ship(Integer orderId, String shipChannel, String shipSn) {
        Order order = new Order();
        order.setId(orderId);
        order.setShipChannel(shipChannel);
        order.setShipSn(shipSn);
        order.setShipTime(new Date());
        order.setOrderStatus(Constant.ORDER_SHIPPED);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    /**
     * wx
     * 我的订单初始化
     *
     * @param param
     * @param showType
     * @return
     */
    @Override
    public BasePageData listWx(BaseParam param, Short showType) {
        //获取当前用户信息
        Integer userId = CommonUtil.getUserId();
        List<OrderListVO> orderListVOS = new ArrayList<>();
        //先查找订单的信息
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria orderExampleCriteria = orderExample.createCriteria();
        //根据传入的showType判断要查询的订单状态
        if (showType.equals(Constant.SHOWTPYE_WAIT_PAY)) {//待付款
            //根据orderStatus=101,deleted=false,userId查询
            orderExampleCriteria.andOrderStatusEqualTo(Constant.ORDER_NOT_PAY).andDeletedEqualTo(false).andUserIdEqualTo(userId);
        } else if (showType.equals(Constant.SHOWTPYE_WAIT_SHIP)) {//待发货
            //orderStatus=201
            orderExampleCriteria.andOrderStatusEqualTo(Constant.ORDER_PAID).andDeletedEqualTo(false).andUserIdEqualTo(userId);
        } else if (showType.equals(Constant.SHOWTPYE_WAIT_RECEIVING)) {//待收货
            //orderStatus=301
            orderExampleCriteria.andOrderStatusEqualTo(Constant.ORDER_SHIPPED).andDeletedEqualTo(false).andUserIdEqualTo(userId);
        } else if (showType.equals(Constant.SHOWTPYE_WAIT_COMMENT)) {//待评价
            //orderStatus=401 or 402
            List<Short> shorts = new ArrayList<>();
            shorts.add(Constant.ORDER_USER_RECEIVING);
            shorts.add(Constant.ORDER_SYSTEM_RECEIVING);
            orderExampleCriteria.andOrderStatusIn(shorts).andDeletedEqualTo(false).andUserIdEqualTo(userId);
        } else {//全部订单
            //查询该用户的全部订单
            orderExampleCriteria.andDeletedEqualTo(false).andUserIdEqualTo(userId);
        }
        //按照add_time排序
        param.setSort("add_time");
        param.setOrder("desc");
        //分页
        //插件影响的是查询到的结果
        MarketPageUtil.startPage(param);
        orderExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Order> orderList = orderMapper.selectByExample(orderExample);
        BasePageData data = BasePageData.ok(orderList);

        //遍历订单列表，查询对应的商品
        for (Order order : orderList) {
            OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
            OrderGoodsExample.Criteria orderGoodsExampleCriteria = orderGoodsExample.createCriteria();
            orderGoodsExampleCriteria.andDeletedEqualTo(false).andOrderIdEqualTo(order.getId());
            List<OrderGoods> goodsList = orderGoodsMapper.selectByExample(orderGoodsExample);
            //根据订单状态判断handleOptionVO和orderStatusText
            Short orderStatus = order.getOrderStatus();
            OrderListVO orderListVO = new OrderListVO(order.getAftersaleStatus(), order.getOrderSn(), order.getActualPrice(), goodsList, order.getId());
            OrderHandleOptionVO handleOptionVO = new OrderHandleOptionVO();
            setParameter(orderStatus, orderListVO, handleOptionVO);
            orderListVO.setHandleOption(handleOptionVO);
            orderListVOS.add(orderListVO);
        }
        data.setList(orderListVOS);
        return data;
    }

    /**
     * 订单详情
     *
     * @param orderId
     * @return
     */
    @Override
    public OrderDetailWxVO detailWx(Integer orderId) {
        //返回参数
        OrderDetailWxVO orderDetailWxVO = new OrderDetailWxVO();
        //主键查询订单
        Order order = orderMapper.selectByPrimaryKey(orderId);
        //获取返回参数的成员OrderDetailInfoWxVO
        Short orderStatus = order.getOrderStatus();
        OrderDetailInfoWxVO orderInfoWxVO = new OrderDetailInfoWxVO(order.getAftersaleStatus(), order.getOrderSn(), order.getActualPrice(),
                null, order.getId(), order.getConsignee(), order.getAddress(), order.getAddTime(), order.getMobile(), order.getMessage(),
                order.getGoodsPrice(), order.getCouponPrice(), order.getFreightPrice(), order.getShipChannel(), order.getShipChannel(), order.getShipSn());
        OrderHandleOptionVO handleOptionVO = new OrderHandleOptionVO();
        setParameter(orderStatus, orderInfoWxVO, handleOptionVO);
        orderInfoWxVO.setHandleOption(handleOptionVO);
        //获取List<OrderGoods>
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria orderGoodsExampleCriteria = orderGoodsExample.createCriteria();
        orderGoodsExampleCriteria.andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

        //封装
        orderDetailWxVO.setOrderInfo(orderInfoWxVO);
        orderDetailWxVO.setOrderGoods(orderGoods);
        return orderDetailWxVO;
    }

    /**
     * 取消订单
     *
     * @param orderId
     */
    @Override
    public void cancel(Integer orderId) {
        Order order = new Order();
        order.setId(orderId);
        order.setUpdateTime(new Date());
        order.setOrderStatus(Constant.ORDER_USER_CANCELLED);//102
        orderMapper.updateByPrimaryKeySelective(order);
    }

    /**
     * 申请退款
     *
     * @param orderId
     */
    @Override
    public void refundWx(Integer orderId) {
        Order order = new Order();
        order.setId(orderId);
        order.setUpdateTime(new Date());
        order.setOrderStatus(Constant.ORDER_REFUND);//202  申请退款
        orderMapper.updateByPrimaryKeySelective(order);
    }

    /**
     * 确认收货
     *
     * @param orderId
     */
    @Override
    public void confirm(Integer orderId) {
        Order order = new Order();
        order.setId(orderId);
        order.setUpdateTime(new Date());
        order.setOrderStatus(Constant.ORDER_USER_RECEIVING);//401 用户收货
        orderMapper.updateByPrimaryKeySelective(order);
    }

    /**
     * 删除订单
     *
     * @param orderId
     */
    @Override
    public void deleteWx(Integer orderId) {
        Order order = new Order();
        order.setId(orderId);
        order.setUpdateTime(new Date());
        order.setDeleted(true);
        orderMapper.updateByPrimaryKeySelective(order);
        //将订单中的商品删除
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria orderGoodsExampleCriteria = orderGoodsExample.createCriteria();
        orderGoodsExampleCriteria.andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        OrderGoods orderGoods = new OrderGoods();
        orderGoods.setUpdateTime(new Date());
        orderGoods.setDeleted(true);
        orderGoodsMapper.updateByExampleSelective(orderGoods, orderGoodsExample);
    }

    /**
     * 获取评价的商品信息
     *
     * @param orderId
     * @param goodsId
     * @return
     */
    @Override
    public OrderGoods goods(Integer orderId, Integer goodsId) {
        OrderGoodsExample example = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId).andGoodsIdEqualTo(goodsId);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(example);
        return orderGoods.get(0);
    }

    /**
     * 评论
     *
     * @param orderCommentVO
     */
    @Override
    public void comment(OrderCommentVO orderCommentVO) {
        Comment comment = new Comment(orderCommentVO.getContent(), orderCommentVO.getHasPicture(), orderCommentVO.getPicUrls(), orderCommentVO.getStar(), new Date(), new Date());
        commentMapper.insertSelective(comment);
        Integer commentId = comment.getId();
        Integer orderGoodsId = orderCommentVO.getOrderGoodsId();
        OrderGoods orderGoods = new OrderGoods();
        orderGoods.setId(orderGoodsId);
        orderGoods.setComment(commentId);
        orderGoods.setUpdateTime(new Date());
        orderGoodsMapper.updateByPrimaryKeySelective(orderGoods);
        //评论之后修改待评价商品数量
        OrderGoods orderGoodsById = orderGoodsMapper.selectByPrimaryKey(orderGoodsId);
        Integer orderId = orderGoodsById.getOrderId();
        Order order = orderMapper.selectByPrimaryKey(orderId);
        int comments = order.getComments().intValue() - 1;
        Order orderNew = new Order();
        orderNew.setId(orderId);
        orderNew.setComments(Short.valueOf(String.valueOf(comments)));
        orderMapper.updateByPrimaryKeySelective(orderNew);
    }

    /**
     * 付款
     *
     * @param orderId
     */
    @Override
    public int prepay(Integer orderId) {
        Order order = new Order();
        order.setId(orderId);
        order.setPayTime(new Date());
//        order.setPayId();
        order.setUpdateTime(new Date());
        order.setOrderStatus(Constant.ORDER_PAID);//已付款
        orderMapper.updateByPrimaryKeySelective(order);
        //付款成功扣库存
        //先拿到订单的商品列表
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria orderGoodsExampleCriteria = orderGoodsExample.createCriteria();
        orderGoodsExampleCriteria.andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        //遍历商品拿到商品规格id并修改库存
        for (OrderGoods orderGood : orderGoods) {
            Integer productId = orderGood.getProductId();
            Product product = productMapper.selectByPrimaryKey(productId);
            Integer numberOld = product.getNumber();//原库存量
            int numberBuy = orderGood.getNumber();
            int numberNew = numberOld - numberBuy;
            //判断库存是否足够
            //如果购买的数量超过库存量，将购买的数量修改成库存剩余量
            //在加入购物车之前购买如果库存不足前端会直接判断库存不足
            Product productIn = new Product();
            productIn.setId(productId);
            productIn.setUpdateTime(new Date());
            productIn.setDeleted(false);
            if (numberNew < 0) {
                productIn.setNumber(0);
            } else {
                productIn.setNumber(numberNew);
            }
            productMapper.updateByPrimaryKeySelective(productIn);
        }
        return Constant.SUCCESS;
    }

    /**
     * 提交订单
     *
     * @param submitInfoVO
     * @return
     */
    @Override
    public OrderSubmitVO submit(OrderSubmitInfoVO submitInfoVO) {
        //拿到userId
        Integer userId = CommonUtil.getUserId();
        //订单编号为日期yyyyMMdd + 6位随机数
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String format = dateFormat.format(new Date());
        String orderSn = format + RandomUtil.getRandomNumber(6);
        //订单编号应具有唯一性
        int count = orderMapper.selectByOrderSn(orderSn);
        while (count != 0) {
            orderSn = format + RandomUtil.GetRandomString(6);
            count = orderMapper.selectByOrderSn(orderSn);
        }
        Short orderStatus = Constant.ORDER_NOT_PAY;//默认未付款状态
        //地址相关的字段
        Integer addressId = submitInfoVO.getAddressId();
        Address address = addressMapper.selectByPrimaryKey(addressId);
        String consignee = address.getName();//收货人名称
        String mobile = address.getTel();//收货人手机号
        String orderAddress = address.getAddressDetail();//收货具体地址

        String message = submitInfoVO.getMessage();

        //价格相关的字段
        double goodsPrice = 0;//商品总费用
        double freightPrice = 0;//配送费用
        double couponPrice = 0;//优惠券减免
        double integralPrice = 0;//用户积分减免
        double grouponPrice = 0;//团购优惠价减免

        Short comments;

        Integer cartId = submitInfoVO.getCartId();
        List<Cart> carts = null;//用来接收购物车中下单的商品
        Cart cart = null;//用来接收直接购买下单的商品
        if (cartId == Constant.BUY_CART) {//购物车中的下单
            CartExample cartExample = new CartExample();
            CartExample.Criteria cartExampleCriteria = cartExample.createCriteria();
            cartExampleCriteria.andUserIdEqualTo(userId).andDeletedEqualTo(false).andCheckedEqualTo(true);//true表示已选中的商品
            carts = cartMapper.selectByExample(cartExample);
            for (Cart cart1 : carts) {
                //遍历获取每个商品的规格
                Integer productId = cart1.getProductId();
                //根据规格得到商品单价
                Product product = productMapper.selectByPrimaryKey(productId);
                BigDecimal price = product.getPrice();
                //商品单价*商品数量
                BigDecimal multiply = price.multiply(BigDecimal.valueOf(cart1.getNumber().longValue()));
                //商品总费用=商品单价*商品数量+其他商品的单价*数量
                goodsPrice += multiply.doubleValue();
            }
            //待评价商品数量
            comments = Short.valueOf(String.valueOf(carts.size()));
        } else {
            //通过商品直接下单
            cart = cartMapper.selectByPrimaryKey(cartId);
            Product product = productMapper.selectByPrimaryKey(cart.getProductId());
            //单个商品时的商品总价
            goodsPrice = product.getPrice().multiply(BigDecimal.valueOf(cart.getNumber().longValue())).doubleValue();
            //单个商品评价数
            comments = 1;
        }
        //查询运费及最低消费
        ExpressConfigDataVO expressConfigDataVO = systemService.queryExpressConfig();
        Double freightMin = Double.valueOf(expressConfigDataVO.getMarket_express_freight_min());//最低消费
        Double freightValue = Double.valueOf(expressConfigDataVO.getMarket_express_freight_value());//运费
        //如果商品总价小于最低消费，需要支付运费
        if (goodsPrice < freightMin) {
            freightPrice = freightValue;
        }
        //查询优惠券相关
        Integer couponId = submitInfoVO.getCouponId();//使用的优惠券id
        //如果使用了优惠券，查询优惠金额
        if (couponId != Constant.NOT_COUPON) {
            Coupon coupon = couponMapper.selectByPrimaryKey(couponId);
            couponPrice = coupon.getDiscount().doubleValue();
        }
        //计算订单费用和实付费用
        double orderPrice = goodsPrice + freightPrice - couponPrice;//订单费用
        double actualPrice = orderPrice - integralPrice;//实付费用

        // 创建订单需要的字段：
        // userId,orderSn,orderStatus,consignee,mobile,address,message
        // goodsPrice,freightPrice,couponPrice,integralPrice,grouponPrice,orderPrice,actualPrice
        // comments,addTime
        Order order = new Order(userId, orderSn, orderStatus, consignee, mobile, orderAddress, message, BigDecimal.valueOf(goodsPrice),
                BigDecimal.valueOf(freightPrice), BigDecimal.valueOf(couponPrice), BigDecimal.valueOf(integralPrice),
                BigDecimal.valueOf(grouponPrice), BigDecimal.valueOf(orderPrice), BigDecimal.valueOf(actualPrice),
                comments,new Date());
        orderMapper.insertSelective(order);

        Integer orderId = order.getId();//useGeneratedKeys="true"获得订单id
        //如果使用了优惠券，要修改用户优惠券表中的相关数据
        if (couponId != Constant.NOT_COUPON) {
            CouponUser couponUser = new CouponUser();
            couponUser.setStatus(Constant.COUPON_USED);//优惠券状态：1，已使用
            couponUser.setUsedTime(new Date());
            couponUser.setOrderId(orderId);
            couponUserMapper.updateByPrimaryKeySelective(couponUser);
        }
        //订单生成后把相关的商品维护到order_goods表中
        //购物车中的下单
        if (cartId == Constant.BUY_CART) {
            for (Cart cart1 : carts) {
                //将购物车中的商品删除
                cartMapper.deleteByPrimaryKey(cart1.getId());
                insertOrderGoods(orderId, cart1);
            }
        } else {
            //通过商品直接下单
            insertOrderGoods(orderId, cart);
        }

        OrderSubmitVO orderSubmitVO = new OrderSubmitVO();
        orderSubmitVO.setOrderId(orderId);
        orderSubmitVO.setGrouponLinkId(0);//没用到的功能
        return orderSubmitVO;
    }

    /**
     * 新增订单商品
     *
     * @param orderId
     * @param cart1
     */
    private void insertOrderGoods(Integer orderId, Cart cart1) {
        OrderGoods orderGoods = new OrderGoods(orderId, cart1.getGoodsId(), cart1.getGoodsName(), cart1.getGoodsSn(), cart1.getProductId(),
                cart1.getNumber(), BigDecimal.valueOf(cart1.getPrice()), cart1.getSpecifications(), cart1.getPicUrl(),
                new Date(), new Date());
        orderGoodsMapper.insertSelective(orderGoods);
    }

    /**
     * 赋值
     *
     * @param orderStatus
     * @param orderListVO
     * @param handleOptionVO
     */
    private void setParameter(Short orderStatus, OrderListVO orderListVO, OrderHandleOptionVO handleOptionVO) {
        switch (orderStatus) {
            case Constant.ORDER_NOT_PAY://101
                orderListVO.setOrderStatusText(OrderStatus.ORDER_NOT_PAY.getOrderStatusText());
                handleOptionVO.setCancel(true);
                handleOptionVO.setPay(true);
                break;
            case Constant.ORDER_USER_CANCELLED://102
                orderListVO.setOrderStatusText(OrderStatus.ORDER_USER_CANCELLED.getOrderStatusText());
                handleOptionVO.setDelete(true);
                break;
            case Constant.ORDER_SYSTEM_CANCELLED://103
                orderListVO.setOrderStatusText(OrderStatus.ORDER_SYSTEM_CANCELLED.getOrderStatusText());
                handleOptionVO.setDelete(true);
                break;
            case Constant.ORDER_PAID://201
                orderListVO.setOrderStatusText(OrderStatus.ORDER_PAID.getOrderStatusText());
                handleOptionVO.setRefund(true);
                break;
            case Constant.ORDER_REFUND://202
                orderListVO.setOrderStatusText(OrderStatus.ORDER_REFUND.getOrderStatusText());
                break;
            case Constant.ORDER_REFUNDED://203
                orderListVO.setOrderStatusText(OrderStatus.ORDER_REFUNDED.getOrderStatusText());
                handleOptionVO.setDelete(true);
                break;
            case Constant.ORDER_SHIPPED://301
                orderListVO.setOrderStatusText(OrderStatus.ORDER_SHIPPED.getOrderStatusText());
                handleOptionVO.setConfirm(true);
                break;
            case Constant.ORDER_USER_RECEIVING://401
                orderListVO.setOrderStatusText(OrderStatus.ORDER_USER_RECEIVING.getOrderStatusText());
                handleOptionVO.setAftersale(true);
                handleOptionVO.setComment(true);
                handleOptionVO.setDelete(true);
                handleOptionVO.setRebuy(true);
                break;
            case Constant.ORDER_SYSTEM_RECEIVING://402
                orderListVO.setOrderStatusText(OrderStatus.ORDER_SYSTEM_RECEIVING.getOrderStatusText());
                handleOptionVO.setAftersale(true);
                handleOptionVO.setComment(true);
                handleOptionVO.setDelete(true);
                handleOptionVO.setRebuy(true);
                break;
        }
    }

    @Override
    public Integer countUncommentByUserId(Integer userId) {

        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();

        criteria.andUserIdEqualTo(userId).andCommentsGreaterThan((short)0);

        List<Order> orders = orderMapper.selectByExample(orderExample);
        int sum= 0;
        for (Order order : orders) {
            sum+= order.getComments();
        }

        return sum;
    }

    @Override
    public Integer countUnpaidByUserId(Integer userId) {
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();

        criteria.andUserIdEqualTo(userId).andOrderStatusEqualTo(((short) 101));
        long l = orderMapper.countByExample(orderExample);

        return ((int) l);
    }

    @Override
    public Integer countUnrecvByUserId(Integer userId) {
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();

        criteria.andUserIdEqualTo(userId).andOrderStatusEqualTo(((short) 301));
        long l = orderMapper.countByExample(orderExample);

        return ((int) l);
    }

    @Override
    public Integer countUnshipByUserId(Integer userId) {
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();

        criteria.andUserIdEqualTo(userId).andOrderStatusEqualTo(((short) 201));
        long l = orderMapper.countByExample(orderExample);

        return ((int) l);
    }
}
